package com.baseweb.webadmin.controller.app;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.model.pojo.Invitation;
import com.baseweb.webadmin.core.model.pojo.MyInvitation;
import com.baseweb.webadmin.core.model.pojo.Schedule;
import com.baseweb.webadmin.core.model.pojo.UserInvitation;
import com.baseweb.webadmin.core.util.MD5Util;
import com.baseweb.webadmin.core.util.RandCharUtil;
import com.baseweb.webadmin.core.util.UploadUtil;
import com.baseweb.webadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * Created by dengsy on 2017/9/3.
 */
@Controller
@RequestMapping(value = "/appApiInvitation")
public class InvitaionAppController extends BaseController {
    @Autowired
    private UserService userService;

    /**
     * 添加我的请柬模板
     * */
    @ResponseBody
    @RequestMapping(value = "/insertMyInvitation", method = RequestMethod.POST)
    public Map<String,Object> insertMyInvitation(@ModelAttribute()MyInvitation myInvitation){
        MyInvitation myInvitationR = userService.findMyInvitaitonByInvitationUser(myInvitation.getInvitationid(),myInvitation.getUserid());
        if(myInvitationR!=null){
            return fail("用户已经存在请柬模板");
        }else {
            myInvitation.setStatus("1");
            if(userService.insertMyInvitation(myInvitation)>0){
                return success(myInvitation,"添加成功");
            }else {
                return fail("添加失败");
            }
        }
    }

    /**
     * 修改我的请柬模板
     * */
    @ResponseBody
    @RequestMapping(value = "/updateMyInvitation", method = RequestMethod.POST)
    public Map<String,Object> updateMyInvitation(@ModelAttribute()MyInvitation myInvitation){
        MyInvitation myInvitationR = userService.findMyInvitaitonById(myInvitation.getId());
        myInvitationR.setInvitationid(myInvitation.getInvitationid());
        myInvitationR.setUserid(myInvitation.getUserid());
        if(myInvitation.getRemark()!=null && !"".equals(myInvitation.getRemark())){
            myInvitationR.setRemark(myInvitation.getRemark());
        }
        if(userService.updateMyInvitation(myInvitationR)>0){
            return success(myInvitationR,"修改成功");
        }else {
            return fail("修改失败");
        }
    }

    /**
     * 删除请柬模板根据id
     * */
    @ResponseBody
    @RequestMapping(value = "/deleteMyInvitationById", method = RequestMethod.GET)
    public Map<String,Object> deleteMyInvitationById(@RequestParam("id") int id){
        if(userService.deleteMyInvitationById(id)>0){
            return success("删除成功");
        }else {
            return fail("删除失败");
        }
    }

    /**
     * 清空我的请柬模板
     * */
    @ResponseBody
    @RequestMapping(value = "/deleteMyInvitationByUserId", method = RequestMethod.GET)
    public Map<String,Object> deleteMyInvitationByUserId(@RequestParam("userid") int userid){
        if(userService.deleteMyInvitationByUserId(userid)>0){
            return success("删除成功");
        }else {
            return fail("删除失败");
        }
    }

    /**
     * 查询我的请柬
     * */
    @ResponseBody
    @RequestMapping(value = "/findUserInvitaiton", method = RequestMethod.GET)
    public Map<String,Object> findUserInvitaiton(@RequestParam("userid") int userid){
        return success(userService.findUserInvitaiton(userid),"查询成功");
    }

    /**
     * 查询所有请柬模板
     * */
    @ResponseBody
    @RequestMapping(value = "/findInvitation", method = RequestMethod.GET)
    public Map<String,Object> findInvitation(){
        return success(userService.findInvitation(),"查询成功");
    }

    /**
     * 添加请柬
     * */
    @ResponseBody
    @RequestMapping(value = "/insertUserInvitation", method = RequestMethod.POST)
    public Map<String,Object> insertUserInvitation(@ModelAttribute()UserInvitation userInvitation,
                                                   @RequestParam(value = "file", required = false) MultipartFile[] file){
        UserInvitation userInvitationR = userService.findUserInvitationByUserIdInvitationId(userInvitation.getUserid(),userInvitation.getInvitationid());
        if(userInvitationR!=null){
            return fail("用户已经存在该请柬");
        }else{
            try{
                //图片上传
                int i = 1;
                for (MultipartFile mf : file) {
                    if (mf != null && mf.getSize() > 0){
                        String avatar_img_url = MD5Util.Md5(System.currentTimeMillis() + RandCharUtil.getRandStr(8) + MD5Util.Md5(mf.getOriginalFilename()))+i+".jpg";
                        UploadUtil.uploadData(mf.getBytes(), avatar_img_url);
                        if(i == 1){
                            userInvitation.setInvitationimg1(avatar_img_url);
                        }else if(i == 2){
                            userInvitation.setInvitationimg2(avatar_img_url);
                        }else if(i == 3){
                            userInvitation.setInvitationimg3(avatar_img_url);
                        }else if(i == 4){
                            userInvitation.setInvitationimg4(avatar_img_url);
                        }else if(i == 5){
                            userInvitation.setInvitationimg5(avatar_img_url);
                        }else if(i == 6){
                            userInvitation.setInvitationimg6(avatar_img_url);
                        }else if(i == 7){
                            userInvitation.setInvitationimg7(avatar_img_url);
                        }
                        i++;
                    }
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
            if(userService.insertUserInvitation(userInvitation) > 0){
                return success(userInvitation,"添加成功");
            }else {
                return fail("添加失败");
            }
        }
    }

    /**
     * 修改请柬
     * */
    @ResponseBody
    @RequestMapping(value = "/updateUserInvitation", method = RequestMethod.POST)
    public Map<String,Object> updateUserInvitation(@ModelAttribute()UserInvitation userInvitation,
                                                   @RequestParam(value = "file", required = false) MultipartFile[] file){
        UserInvitation userInvitationR = userService.findUserInvitationById(userInvitation.getId());

        if(userInvitation.getGroom()!=null && !"".equals(userInvitation.getGroom())){
            userInvitationR.setGroom(userInvitation.getGroom());
        }
        if(userInvitation.getBride()!=null && !"".equals(userInvitation.getBride())){
            userInvitationR.setBride(userInvitation.getBride());
        }
        if(userInvitation.getInvitationdate()!=null && !"".equals(userInvitation.getInvitationdate())){
            userInvitationR.setInvitationdate(userInvitation.getInvitationdate());
        }
        if(userInvitation.getInvitationhotel()!=null && !"".equals(userInvitation.getInvitationhotel())){
            userInvitationR.setInvitationhotel(userInvitation.getInvitationhotel());
        }
        if(userInvitation.getRemark()!=null && !"".equals(userInvitation.getRemark())){
            userInvitationR.setRemark(userInvitation.getRemark());
        }
        if(userInvitation.getAdress()!=null && !"".equals(userInvitation.getAdress())){
            userInvitationR.setAdress(userInvitation.getAdress());
        }

        try{
            //图片上传
            int i = 1;
            for (MultipartFile mf : file) {
                if (mf != null && mf.getSize() > 0){
                    String avatar_img_url = MD5Util.Md5(System.currentTimeMillis() + RandCharUtil.getRandStr(8) + MD5Util.Md5(mf.getOriginalFilename()))+i+".jpg";
                    UploadUtil.uploadData(mf.getBytes(), avatar_img_url);
                    if(i == 1){
                        userInvitationR.setInvitationimg1(avatar_img_url);
                    }else if(i == 2){
                        userInvitationR.setInvitationimg2(avatar_img_url);
                    }else if(i == 3){
                        userInvitationR.setInvitationimg3(avatar_img_url);
                    }else if(i == 4){
                        userInvitationR.setInvitationimg4(avatar_img_url);
                    }else if(i == 5){
                        userInvitationR.setInvitationimg5(avatar_img_url);
                    }else if(i == 6){
                        userInvitationR.setInvitationimg6(avatar_img_url);
                    }else if(i == 7){
                        userInvitationR.setInvitationimg7(avatar_img_url);
                    }
                    i++;
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }

        if(userService.updateUserInvitation(userInvitationR)>0){
            return success(userService.findUserInvitationById(userInvitationR.getId()),"修改成功");
        }else {
            return fail("修改失败");
        }
    }

    /**
     * 查询用户请柬
     * */
    @RequestMapping(value = "/findInvitaionByUserId", method = RequestMethod.GET)
    public String findInvitaionByUserId(Model model,@RequestParam("id") int id){
        UserInvitation userInvitation = userService.findUserInvitationById(id);
        if(userInvitation.getInvitationid() == 1){
//            return "redirect:/invitation/invitationOneHtml";
//            return success("/invitation/invitationOneHtml","修改成功");
            model.addAttribute("url","/invitation/invitationOneHtml?userInvitationId="+id);
            model.addAttribute("userInvitationId",id);
        }else {
//            return "redirect:/invitation/invitationOneHtml";
//            return success("/invitation/invitationOneHtml","修改成功");
            model.addAttribute("url","/invitation/invitationOneHtml?userInvitationId="+id);
            model.addAttribute("userInvitationId",id);
        }
        return "";
    }

    /**
     * 查询请柬模板
     * */
    @RequestMapping(value = "/findInvitationById", method = RequestMethod.GET)
    public String findInvitationById(Model model,@RequestParam("id") int id){
        if(id == 1){
            model.addAttribute("url","/invitation/invitationOneHtml?userInvitationId=-1");
            model.addAttribute("invitationId",id);
//            return success("/invitation/invitationOneHtml","修改成功");
        }else {
//            return success("/invitation/invitationOneHtml","修改成功");
            model.addAttribute("url","/invitation/invitationOneHtml?userInvitationId=-1");
            model.addAttribute("invitationId",id);
        }
        return "";
    }

    /**
     * 查询请柬内容根据id
     * */
    @RequestMapping(value = "/findUserInvitationByUserIdInvitationId", method = RequestMethod.POST)
    public String findUserInvitationByUserIdInvitationId(Model model,@RequestParam("userInvitationId") int userInvitationId){
        model.addAttribute("userInvitation",userService.findUserInvitationById(userInvitationId));
        return "";
    }

    /**
     * 删除请柬根据Id
     * */
    @ResponseBody
    @RequestMapping(value = "/deleteUserInvitation", method = RequestMethod.GET)
    public Map<String,Object> deleteUserInvitation(@RequestParam("id") int id){
        if(userService.deleteUserInvitation(id)>0){
            return success("删除成功");
        }else {
            return fail("删除失败");
        }
    }
}
